package pl.elka.simulator.core;

import java.util.ArrayList;
import java.util.List;

public class CollisionSolver {
	private List<Constraint> constraints;;
	private AreaConstraint area;
	
	public CollisionSolver(EnviromentConfiguration ec) {
		constraints = new ArrayList<Constraint>(ec.getConstraints());
		constraints.add(ec.getAreaConstraint());
		
	}
	public Position findCollisionPosition(Position currentPosition, Position newPosition){			
		Position nearestCollision = null;
		for(Constraint constraint:constraints){
			Position collisionPosition = constraint.findCollisionPoint(currentPosition, newPosition);		
			nearestCollision = Constraint.chooseNearerPosition(currentPosition, nearestCollision, collisionPosition);							
		}
		return nearestCollision;
	}
	public boolean isInConstrain(Node n){
		for(Constraint constraint:constraints){
			if(constraint.contains(n.position)){
				return true;
			}
		}
		return false;
	}
	public Segment findCollisionSegment(Position cPosition, Position nPosition){
		Segment collision = null;//area.findCollisionSegment(cPosition, nPosition);
		if(collision==null){
			for(Constraint constraint:constraints){
				collision = constraint.findCollisionSegment(cPosition, nPosition);
				if(collision!=null){					
					break;
				}
			}
		}
		
		return collision;		
	}
}
